package com.kilomob.grady.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.kilomob.grady.server.netty.NettyServer;

/**
 * 
 * service解析类
 * 
 *   
 * @author zhouzhixiang  
 * @date 2016年12月31日
 * @since 1.0
 */
public class ServiceBean implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {
    private static Logger LOGGER = LoggerFactory.getLogger(ServiceBean.class);
    
    private String interfaceName;
    private String ref;
    private ApplicationContext applicationContext;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        Object service = applicationContext.getBean(ref);
        if(service == null ){
            LOGGER.warn("service ref is null !!!");
        }else{
            NettyServer.getHandlerMap().put(interfaceName, applicationContext.getBean(ref));
        }
    }

    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;
        applicationContext.publishEvent(new ServerStartEvent(new Object()));
    }
    
    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }
    
    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }
    
}
